package com.core.dto;

import com.core.model.user.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 2016-01-08
 * Time: 11:20
 * To change this template use Editor | File and Code Templates.
 */
public class UserDTO {

    @NotNull
    @Size(min = 2, max = 20, message = "guid必须在{min}与{max}之间的长度!")
    private String guid;
    @NotNull
    @Size(min = 2, max = 20, message = "username必须在{min}与{max}之间的长度!")
    private String username;
    @NotNull
    @Size(min = 6, max = 20, message = "password必须在{min}与{max}之间的长度!")
    private String password;
    @NotNull
    @Size(min = 2, max = 20, message = "nickname必须在{min}与{max}之间的长度!")
    private String nickname;
    private String validateCode;
    private List<RoleDTO> roleDTOs;
    private List<String> roleGuids;

    public UserDTO() {
    }


    public UserDTO(String guid, String username, String nickname) {
        this.guid = guid;
        this.username = username;
        this.nickname = nickname;
    }


    public UserDTO(String guid, String username, String nickname, List<String> roleGuids) {
        this.guid = guid;
        this.username = username;
        this.nickname = nickname;
        this.roleGuids = roleGuids;
    }

    public static UserDTO toDTO(User user) {
        List<String> roleGuids = new ArrayList<>();
        for (int i = 0; i < user.roles().size(); i++) {
            roleGuids.add(user.roles().get(i).getRole().guid());
        }
        return new UserDTO(user.guid(), user.getUsername(), user.userInfo().nickname(), roleGuids);
    }

    public static List<UserDTO> toDTOs(List<User> users) {
        List<UserDTO> userDTOs = new ArrayList<>();
        for (int i = 0; i < users.size(); i++) {
            userDTOs.add(toDTO(users.get(i)));
        }
        return userDTOs;
    }

    /**
     * 测试方法,每次new Customer()
     * 正式环境不能手动添加普通用户
     *
     * @return
     */
    public static List<UserRole> toUserRole(UserDTO userDTO, List<Role> roles) {
        User user = new Customer(userDTO.getUsername(), userDTO.getPassword());
        user.initUserInfo(new UserInfo(user, userDTO.getNickname()));
        List<UserRole> userRoles = new ArrayList<>();
        for (int i = 0; i < roles.size(); i++) {
            userRoles.add(new UserRole(user, roles.get(i)));
        }
        return userRoles;
    }

    public static List<UserRole> toUserRole(User oldUser, UserDTO userDTO, List<Role> roles) {
        oldUser.userInfo().initUserInfo(userDTO);
        List<UserRole> userRoles = new ArrayList<>();
        for (int i = 0; i < roles.size(); i++) {
            userRoles.add(new UserRole(oldUser, roles.get(i)));
        }
        return userRoles;
    }


    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getValidateCode() {
        return validateCode;
    }

    public void setValidateCode(String validateCode) {
        this.validateCode = validateCode;
    }

    public List<String> getRoleGuids() {
        return roleGuids;
    }

    public void setRoleGuids(List<String> roleGuids) {
        this.roleGuids = roleGuids;
    }

    public List<RoleDTO> getRoleDTOs() {
        return roleDTOs;
    }

    public void setRoleDTOs(List<RoleDTO> roleDTOs) {
        this.roleDTOs = roleDTOs;
    }
}
